﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Back.Xinkui.Entity.Company;
using Back.Xinkui.Common;
using DataFrameworkAccess;
using CommonFramework.Utility;
using System.Transactions;
namespace Back.Xinkui.DataAccess.Company
{
    public class CompanyDataAccess
    {
        public static List<CompanyListEntity> GetCompanyList(string where, Pager page)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetCompanyList");
            cmd.SetParameterValue("@PageIndex", page.PageIndex);
            cmd.SetParameterValue("@WHERE", where);
            cmd.SetParameterValue("@PageSize", page.PageSize);
            var list = cmd.ExecuteEntityList<CompanyListEntity>();

            page.TotalCount = cmd.GetParameterValue("@AllCount").TryType<int>(0);
            return list;
        }

        public static List<ProdcutEntity> GetProductList(string where, Pager page)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetProductList");
            cmd.SetParameterValue("@PageIndex", page.PageIndex);
            cmd.SetParameterValue("@WHERE", where);
            cmd.SetParameterValue("@PageSize", page.PageSize);
            var list = cmd.ExecuteEntityList<ProdcutEntity>();

            page.TotalCount = cmd.GetParameterValue("@AllCount").TryType<int>(0);
            return list;
        }

        public static ProdcutEntity GetProductByProductId(int productId)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Product.Query.GetProductByProductId");
            cmd.SetParameterValue("@ProductID", productId);

            return cmd.ExecuteEntity<ProdcutEntity>();
        }



        public static CompanyDetailEntity GetCompanyInfoByCompanyID(int companyID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetCompanyInfoByCompanyID");
            cmd.SetParameterValue("@CompanyID", companyID);

            return cmd.ExecuteEntity<CompanyDetailEntity>();
        }

        public static UserEntity GetCompanyUserByCompanyID(int companyID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetCompanyUserByCompanyID");
            cmd.SetParameterValue("@CompanyID", companyID);

            return cmd.ExecuteEntity<UserEntity>();
        }

        public static void SaveCompanyInfo(CompanyDetailEntity entity, UserEntity userEntity)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {

                    DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.UpdateCompanyInfo");
                    cmd.SetParameterValue("@CompanyID", entity.CompanyID);
                    cmd.SetParameterValue("@CatalogId", entity.CatalogId);
                    cmd.SetParameterValue("@CompanyName", entity.CompanyName);
                    cmd.SetParameterValue("@CnCompanyName", entity.CnCompanyName);
                    cmd.SetParameterValue("@KeyWord", entity.KeyWord);
                    cmd.SetParameterValue("@CompanyDescription", entity.CompanyDescription);
                    cmd.SetParameterValue("@MainProduct", entity.MainProduct);
                    cmd.SetParameterValue("@CompanyEmail", entity.CompanyEmail);
                    cmd.SetParameterValue("@CountryID", entity.CountryID);
                    cmd.SetParameterValue("@ProvinceID", entity.ProvinceID);
                    cmd.SetParameterValue("@CityID", entity.CityID);
                    cmd.SetParameterValue("@StreetAddress", entity.StreetAddress);
                    cmd.SetParameterValue("@PostCode", entity.PostCode);
                    cmd.SetParameterValue("@NumberofEmployees", entity.NumberofEmployees);
                    cmd.SetParameterValue("@NumberofRDEmployees", entity.NumberofRDEmployees);
                    cmd.SetParameterValue("@Ceo", entity.Ceo);
                    cmd.SetParameterValue("@YearEstabished", entity.YearEstabished);
                    cmd.SetParameterValue("@IsExport", entity.IsExport);
                    cmd.SetParameterValue("@ExportMarket", entity.ExportMarket);
                    cmd.SetParameterValue("@RegisterCapital", entity.RegisterCapital);
                    cmd.SetParameterValue("@TotalAnnualSales", entity.TotalAnnualSales);
                    cmd.SetParameterValue("@ExportRatio", entity.ExportRatio);
                    cmd.SetParameterValue("@TypicalClients", entity.TypicalClients);
                    cmd.SetParameterValue("@OwnershipType", entity.OwnershipType);
                    cmd.SetParameterValue("@PhoneNum", entity.PhoneNum);
                    cmd.SetParameterValue("@FaxNum", entity.FaxNum);
                    cmd.SetParameterValue("@WebSite", entity.WebSite);
                    cmd.SetParameterValue("@MemberType", entity.MemberType);
                    cmd.SetParameterValue("@UpdateDate", entity.UpdateDate);
                    cmd.SetParameterValue("@Logo", entity.Logo);
                    cmd.SetParameterValue("@ImagePhoto", entity.ImagePhoto);
                    cmd.SetParameterValue("@IsOE", entity.IsOE);
                    cmd.SetParameterValue("@BussinessType", entity.BussinessType);

                    cmd.SetParameterValue("@UserID", userEntity.UserID);
                    cmd.SetParameterValue("@FirstName", userEntity.FirstName);
                    cmd.SetParameterValue("@LastName", userEntity.LastName);
                    cmd.SetParameterValue("@UserEmail", userEntity.Email);
                    cmd.SetParameterValue("@Gender", userEntity.Gender);
                    cmd.SetParameterValue("@Department", userEntity.Department);
                    cmd.SetParameterValue("@JobTitle", userEntity.JobTitle);
                    cmd.SetParameterValue("@UserPhoneNum", userEntity.PhoneNum);
                    cmd.SetParameterValue("@UserFaxNum", userEntity.FaxNum);
                    cmd.SetParameterValue("@UserMobile", userEntity.Mobile);
                    cmd.ExecuteNonQuery();

                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }

        }

        public static void CreateCompanyInfo(CompanyDetailEntity entity, UserEntity userEntity)
        {

            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.CreateCompanyInfo");

                    cmd.SetParameterValue("@CatalogId", entity.CatalogId);
                    cmd.SetParameterValue("@CompanyName", entity.CompanyName);
                    cmd.SetParameterValue("@CnCompanyName", entity.CnCompanyName);
                    cmd.SetParameterValue("@KeyWord", entity.KeyWord);
                    cmd.SetParameterValue("@CompanyDescription", entity.CompanyDescription);
                    cmd.SetParameterValue("@MainProduct", entity.MainProduct);
                    cmd.SetParameterValue("@CompanyEmail", entity.CompanyEmail);
                    cmd.SetParameterValue("@CountryID", entity.CountryID);
                    cmd.SetParameterValue("@ProvinceID", entity.ProvinceID);
                    cmd.SetParameterValue("@CityID", entity.CityID);
                    cmd.SetParameterValue("@StreetAddress", entity.StreetAddress);
                    cmd.SetParameterValue("@PostCode", entity.PostCode);
                    cmd.SetParameterValue("@NumberofEmployees", entity.NumberofEmployees);
                    cmd.SetParameterValue("@NumberofRDEmployees", entity.NumberofRDEmployees);
                    cmd.SetParameterValue("@Ceo", entity.Ceo);
                    cmd.SetParameterValue("@YearEstabished", entity.YearEstabished);
                    cmd.SetParameterValue("@IsExport", entity.IsExport);
                    cmd.SetParameterValue("@ExportMarket", entity.ExportMarket);
                    cmd.SetParameterValue("@RegisterCapital", entity.RegisterCapital);
                    cmd.SetParameterValue("@TotalAnnualSales", entity.TotalAnnualSales);
                    cmd.SetParameterValue("@ExportRatio", entity.ExportRatio);
                    cmd.SetParameterValue("@TypicalClients", entity.TypicalClients);
                    cmd.SetParameterValue("@OwnershipType", entity.OwnershipType);
                    cmd.SetParameterValue("@PhoneNum", entity.PhoneNum);
                    cmd.SetParameterValue("@FaxNum", entity.FaxNum);
                    cmd.SetParameterValue("@WebSite", entity.WebSite);
                    cmd.SetParameterValue("@MemberType", entity.MemberType);
                    cmd.SetParameterValue("@Logo", entity.Logo);
                    cmd.SetParameterValue("@ImagePhoto", entity.ImagePhoto);
                    cmd.SetParameterValue("@IsOE", entity.IsOE);
                    cmd.SetParameterValue("@BussinessType", entity.BussinessType);
                    cmd.SetParameterValue("@CreateIp", entity.CreateIp);

                    cmd.SetParameterValue("@UserName", userEntity.UserName);
                    cmd.SetParameterValue("@Password", userEntity.Password);
                    cmd.SetParameterValue("@FirstName", userEntity.FirstName);
                    cmd.SetParameterValue("@LastName", userEntity.LastName);
                    cmd.SetParameterValue("@UserEmail", userEntity.Email);
                    cmd.SetParameterValue("@Gender", userEntity.Gender);
                    cmd.SetParameterValue("@Department", userEntity.Department);
                    cmd.SetParameterValue("@JobTitle", userEntity.JobTitle);
                    cmd.SetParameterValue("@UserPhoneNum", userEntity.PhoneNum);
                    cmd.SetParameterValue("@UserFaxNum", userEntity.FaxNum);
                    cmd.SetParameterValue("@UserMobile", userEntity.Mobile);
                    cmd.ExecuteNonQuery();


                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }
        }

        /// <summary>
        /// 获取公司证书信息
        /// </summary>
        /// <param name="companyID"></param>
        /// <returns></returns>
        public static List<CertificateEntity> GetCompanyCertificateByCompanyID(int companyID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetCompanyCertificateByCompanyID");
            cmd.SetParameterValue("@CompanyID", companyID);

            return cmd.ExecuteEntityList<CertificateEntity>();
        }

        /// <summary>
        /// 修改证书信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int EditCertificate(CertificateEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.EditCertificate");

            cmd.SetParameterValue("@CertID", entity.CertID);
            cmd.SetParameterValue("@CertName", entity.CertName);
            cmd.SetParameterValue("@PicPath", entity.PicPath);
            cmd.SetParameterValue("@CompanyID", entity.CompanyID);

            return cmd.ExecuteScalar<int>();
        }

        /// <summary>
        /// 更改公司状态——公司审核
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void UpdateCompanyStatus(int companyID, int status, string UpdateIp)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.UpdateCompanyStatus");
            cmd.SetParameterValue("@CompanyID", companyID);
            cmd.SetParameterValue("@AuditStatus", status);
            cmd.SetParameterValue("@UpdateIP", UpdateIp);

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 删除公司信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void DeleteCompany(int companyID, string UpdateIp)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.DeleteCompany");
            cmd.SetParameterValue("@CompanyID", companyID);
            cmd.SetParameterValue("@UpdateIP", UpdateIp);

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 删除证书信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void DeleteCertificate(int certId)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.DeleteCertificate");

            cmd.SetParameterValue("@CertID", certId);

            cmd.ExecuteNonQuery();
        }


        /// <summary>
        /// 获取公司证书信息
        /// </summary>
        /// <param name="companyID"></param>
        /// <returns></returns>
        public static List<EquipmentEntity> GetCompanyEquipmentByCompanyID(int companyID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Query.GetCompanyEquipmentByCompanyID");
            cmd.SetParameterValue("@CompanyID", companyID);

            return cmd.ExecuteEntityList<EquipmentEntity>();
        }

        /// <summary>
        /// 修改证书信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int EditEquipment(EquipmentEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.EditEquipment");

            cmd.SetParameterValue("@EquipmentID", entity.EquipmentID);
            cmd.SetParameterValue("@EquipmentName", entity.EquipmentName);
            cmd.SetParameterValue("@PicPath", entity.PicPath);
            cmd.SetParameterValue("@CompanyID", entity.CompanyID);

            return cmd.ExecuteScalar<int>();
        }

        /// <summary>
        /// 删除证书信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void DeleteEquipment(int equipmentID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Company.Action.DeleteEquipment");

            cmd.SetParameterValue("@EquipmentID", equipmentID);

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        ///  新增产品信息
        /// </summary>
        public static void CreateProductInfo(ProdcutEntity proEntity)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    DataCommand cmd = DataCommandManager.GetDataCommand("Product.Action.CreateProductInfo");
                    cmd.SetParameterValue("@ProductName", proEntity.ProductName);
                    cmd.SetParameterValue("@CategoryID", proEntity.CategoryID);
                    cmd.SetParameterValue("@CompanyID", proEntity.CompanyID);
                    cmd.SetParameterValue("@ProductDescription", proEntity.ProductDescription);
                    cmd.SetParameterValue("@PlaceOfOrigin", proEntity.PlaceOfOrigin);
                    cmd.SetParameterValue("@MainKeyword", proEntity.MainKeyword);
                    cmd.SetParameterValue("@Keywords", proEntity.Keywords);
                    cmd.SetParameterValue("@OEMNO", proEntity.OEMNO);
                    cmd.SetParameterValue("@CertificateIDS", proEntity.CertificateIDS);
                    cmd.SetParameterValue("@GroupID", proEntity.GroupID);
                    cmd.SetParameterValue("@KeyProducts", proEntity.KeyProducts);
                    cmd.SetParameterValue("@Application", proEntity.Application);
                    cmd.SetParameterValue("@PicImgs", proEntity.PicImgs);
                    cmd.SetParameterValue("@CreateIp", proEntity.CreateIP);
                    cmd.ExecuteNonQuery();

                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }

        }

        /// <summary>
        /// 保存产品信息
        /// </summary>
        public static void SaveProductInfo(ProdcutEntity proEntity)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    DataCommand cmd = DataCommandManager.GetDataCommand("Product.Action.SaveProductInfo");
                    cmd.SetParameterValue("@ProductID", proEntity.ProductID);
                    cmd.SetParameterValue("@ProductName", proEntity.ProductName);
                    cmd.SetParameterValue("@CategoryID", proEntity.CategoryID);
                    cmd.SetParameterValue("@ProductDescription", proEntity.ProductDescription);
                    cmd.SetParameterValue("@PlaceOfOrigin", proEntity.PlaceOfOrigin);
                    cmd.SetParameterValue("@MainKeyword", proEntity.MainKeyword);
                    cmd.SetParameterValue("@Keywords", proEntity.Keywords);
                    cmd.SetParameterValue("@OEMNO", proEntity.OEMNO);
                    cmd.SetParameterValue("@CertificateIDS", proEntity.CertificateIDS);
                    cmd.SetParameterValue("@KeyProducts", proEntity.KeyProducts);
                    cmd.SetParameterValue("@Application", proEntity.Application);
                    cmd.SetParameterValue("@PicImgs", proEntity.PicImgs);
                    cmd.SetParameterValue("@UpdateIP", proEntity.UpdateIP);
                    cmd.ExecuteNonQuery();

                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }
        }
        /// <summary>
        /// 更改产品状态
        /// </summary>
        /// <param name="productID"></param>
        public static void UpdateProductStatus(int productID, int status)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Product.Action.UpdateProductStatus");

            cmd.SetParameterValue("@ProductID", productID);
            cmd.SetParameterValue("@AuditStatus", status);
            cmd.ExecuteNonQuery();
        }

        public static void DeleteProduct(int productID, string updateIP)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Product.Action.DeleteProduct");

            cmd.SetParameterValue("@ProductID", productID);
            cmd.SetParameterValue("@UpdateIP", updateIP);
            cmd.ExecuteNonQuery();
        }
    }
}
